﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using CrawlerCore;
using HtmlAgilityPack;
using WapCrawlerLib;


namespace WapCrawlerCore.Classes
{
    public class Kichhoat
    {
        private BrowserSession_General br = new BrowserSession_General();
        System.Net.CookieCollection cookieCollect;
        private System.Net.Cookie SESID;
        private System.Net.Cookie cuid;
        private System.Net.Cookie cups;

        System.Net.Cookie sid;
        private System.Net.Cookie JSESSIONID;

        public void Login(string username, string password)
        {
            br.Get("http://kichhoat.net/login.php");
            SESID = br.Response.Cookies["SESID"];

            cookieCollect = new System.Net.CookieCollection();
            cookieCollect.Add(SESID);
            br.SaveCookiesFrom(cookieCollect);
            br.PostParams = string.Format("n={0}&p={1}&mem=1",username,password);
            br.Post("http://kichhoat.net/login.php");
            cuid = br.Response.Cookies["cuid"];
            cookieCollect.Add(cuid);
            cups = br.Response.Cookies["cups"];
            cookieCollect.Add(cups);
            
            br.SaveCookiesFrom(cookieCollect);
            br.Get("http://kichhoat.net/login.php");

            br.SaveCookiesFrom(cookieCollect);
            br.Get("http://kichhoat.net/service/tool.php");

            br.SaveCookiesFrom(cookieCollect);
            br.Get("http://sv2.mway.vn:88/KichHoatService/scan.mobi");
            JSESSIONID = br.Response.Cookies["JSESSIONID"];

            br.Get(string.Format("http://sv2.mway.vn:88/KichHoatService/scan.mobi?sid={0}", SESID.Value));
            sid=br.Response.Cookies["sid"];
        }
        

        public Dictionary<string, string> CheckLink(string link)
        {
            cookieCollect = new System.Net.CookieCollection();
            cookieCollect.Add(sid);
            cookieCollect.Add(JSESSIONID);
            br.SaveCookiesFrom(cookieCollect);
            
            //get viewstate
            //link = @"http://giaitri321.pro/game-offline/gamehay/1/motogp-2012/S40_MotoGP_2012.jar";
            br.Get("http://sv2.mway.vn:88/KichHoatService/scan.mobi");
            string paramViewstate = br.HtmlDoc.GetValueInInputTag("hidden", "name", "javax.faces.ViewState");
            br.PostParams = string.Format(@"form_scan={0}&form_scan:jar_link={1}&form_scan:j_idt11={2}&javax.faces.ViewState={3}",
                              "form_scan", System.Web.HttpUtility.UrlEncode(link), System.Web.HttpUtility.UrlEncode("Kiểm tra"), System.Web.HttpUtility.UrlEncode(paramViewstate));
            //form_scan=form_scan
            //form_scan%3Ajar_link=http%3A%2F%2Fgiaitri321.pro%2Fonline%2Favatar%2Ffile%2FAvatar_Time_Animal.jar
            //form_scan%3Aj_idt11=Ki%E1%BB%83m+tra
            //javax.faces.ViewState=H4sIAAAAAAAAAH1UTWwbRRSeODahpqGlQE4UzSGq%2Bre7dmqS2lEojpMQU9up6hIBFTKT3bE9ye7MMjNrr3uI4MYRiRNSEeIOp3LihnJAQi0SSFw40V4RXEEICd6sXceFlJG8np%2F3vvfeN9%2Bbz39FmVBJ9Mwu6RE70sy3N4nq1kmYmfnp64O5d76fRqkNlPUF8TaIq4WsomO6K6nqCt%2BLwyuvIDOO95%2BE70n4TWv0gisCW0XcbhOXKnttwEnA3LKrmeAKYj17GKssJRnUmNLx%2Bz%2Bc%2Fvgb8sk0mqqitGK3aBwa4H7afGONTrWFDFrKJby0S2TLZ3wvAqibtQTMJ7xjb%2B3sUlcvf%2FjdG5%2BeVOf8FPgZjHT0LtpHT8AsNZ5lQhjj1cy%2BROcNTjxKGQoIBadc269XqzyM9Pw1KUIq9eAqHSg0GqcggERPHyawzqNg8jDUKNMjPvPGBCdmq0L4lPB7WL734%2B0%2Ff0uhqbcSuwhqnoJULjwula1I%2F18uppzjegTVkyjdE8xDhyMO943JUxrN%2BsIl%2Fraxa1KtzO4JFIbjCYDstpiXP2TOaOTM0Xk1NdF0E9RAZZP0qHzz2zsrH92%2BV0%2BhVA0dc32iVIMEFK4wuSvHcOA0tWS8s1xDWQU%2BXoKh0dzQggmnSSUD5m6RHZ8ux6EJf%2FZxtGwz2r8uhJ43E1Du36MBxNZQRmnSAegzCXTsJO4OCUOfucQo0gE6jW6axm45IRH9R9VT2tyzrHSpu0flKtweJPTcjvm3X3tk%2F%2FmtB3N3VwovJtWnjUwN4ukamgmoUhAiWYJuTgy9a6LDuHH8%2BYNffm%2Fc%2FeOvJOsZ4roi4npobC7kSlfrsOQ4HUYYcHdpIW%2BHUjiCQwjqQGmaSKfNfOqUk3nrBgtoq8xZQHwbUtep8ysv4yRRvAFmJXy03Wz25oW3Ewu81z34knewe3AHXz34wu3iTXH%2Fq8%2F0hIU5atabs1lrNHC93KhurDdv4Ic71my2TjhrU6WtbSoVMF7CeTs3m22IPUasenXNp9riwqIx0yWsZUTBZbi7TbknZAk3hO6CXCb2x0BF%2B9J4e43Cl1oVwdtMBiVcFwzvEI49yjH0Au6TEBv%2BIAgs%2Bn37UTLHOPmH5GALG3rwkJ6LDnMFt0PeuThKyA4I4%2FaroO4688ATEJgrxbrHjLKGiXQimeishCu1tYqVlH6dtivCgzuQtG3JdtH1B%2FRyZ%2BAOrHxhqZgrvlRYLOSWFgqXFxeXikvgUJEUGsSzVgem5kU711rI47PNiOMkoBooTQOFq9y1z43LMF13dCX%2FyhM6oJ1IAhyvWQt2LoZXaH6y3Sb6xZ7slyOenyyce1Hy2Mf%2FAJlWivBeBgAA";
            br.SaveCookiesFrom(cookieCollect);
            br.Post("http://sv2.mway.vn:88/KichHoatService/scan.mobi",true);
            ////div/pre/text()
            HtmlNodeCollection nodes = br.HtmlDoc.DocumentNode.SelectNodes("//div/pre");
            Dictionary<string, string> result = new Dictionary<string, string>();
            result.Add("IsKichHoat", "0");
            result.Add("IsSMS", "0");
            result.Add("Result", nodes[0].InnerHtml);
            if (br.HtmlDoc.DocumentNode.InnerHtml.Contains(@"[+] File có Kích Hoạt"))
                result["KH"] = "1";
            if (br.HtmlDoc.DocumentNode.InnerHtml.Contains(@"[+] File có SMS"))
                result["SMS"] = "1";

            return result;
        }
    }
}